package com.fafa.pos.dao.impl;

import android.content.ContentValues;
import android.database.Cursor;
import com.fafa.pos.dao.DBUtil;
import com.fafa.pos.dao.IUserInfoDao;
import com.fafa.pos.pojo.CardInfo;
import com.fafa.pos.pojo.UserInfo;
import com.fafa.pos.util.DateUtil;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by ZhouBing on 2017/8/10.
 *
 * SQLite  --- T_USERINFO
 */
public class UserInfoDaoImpl implements IUserInfoDao{
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
    private DBUtil dbUtil;
    private ConfigDaoImpl configDao;

    public UserInfoDaoImpl(DBUtil dbUtil) {
        this.dbUtil = dbUtil;
        configDao = new ConfigDaoImpl(dbUtil);
    }

    @Override
    public void saveUserInfoList(List<UserInfo> userInfos) {

        dbUtil.getDB().beginTransaction();

        String updateDate = "1900-00-00 00:00:00.000";
        try{
            for (UserInfo userInfo:userInfos) {

                //Id存在则更新不存在新增
                String sql = "select USER_ID from T_USERINFO where USER_ID = ?";
                Cursor cursor = dbUtil.getDB().rawQuery(sql,new String[]{userInfo.getUserId()});

                ContentValues contentValues = new ContentValues();
                contentValues.put("USER_ID",userInfo.getUserId());
                contentValues.put("USER_CODE",userInfo.getUserCode());
                contentValues.put("USER_NAME",userInfo.getUserName());
                contentValues.put("PASSWORD",userInfo.getPassword());
                contentValues.put("STATUS",userInfo.getStatus());
                contentValues.put("VISIBLE",userInfo.getVisible());
                contentValues.put("UPDATE_DATE",userInfo.getUpdate());

                Date tempDate = sdf.parse(userInfo.getUpdate());

                if (sdf.parse(updateDate).before(tempDate)) {
                    updateDate=sdf.format(tempDate);
                }

                if(cursor.moveToNext()){
                    dbUtil.getDB().update("T_USERINFO",contentValues," USER_ID = ?",new String[]{userInfo.getUserId()});
                }else{
                    dbUtil.getDB().insert("T_USERINFO",null,contentValues);
                }
                if(cursor!=null){
                    cursor.close();
                }

            }
            configDao.saveItem("T_USERINFO_UPDATE_DATE", updateDate);
            dbUtil.getDB().setTransactionSuccessful();
        } catch (ParseException e) {
            e.printStackTrace();
        } finally {
            dbUtil.getDB().endTransaction();
            dbUtil.close();
        }
    }

    @Override
    public UserInfo checkLoginUser(String userCode, String password) {

        UserInfo userInfo = null;

        Cursor cursor = dbUtil.getDB().rawQuery("select * from T_USERINFO where USER_CODE=? and PASSWORD = ?",new String[]{userCode,password});
        if (cursor.moveToNext()){
            userInfo = new UserInfo();
            userInfo.setUserId(cursor.getString(0));
            userInfo.setUserCode(cursor.getString(1));
            userInfo.setUserName(cursor.getString(2));
            userInfo.setPassword(cursor.getString(3));
            userInfo.setStatus(cursor.getString(4));
            userInfo.setVisible(cursor.getString(5));
            userInfo.setUpdate(cursor.getString(6));
        }
        if(cursor!=null){
            cursor.close();
        }
        dbUtil.close();
        return userInfo;
    }
}
